package com.dyb.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import com.dyb.config.OssProperties;
import com.dyb.entity.TestParam;
import com.dyb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class OssController {
    @Autowired
    private OssProperties properties;

    @Resource
    private AmazonS3 client;

    @GetMapping("/oss")
    public void test3() throws IOException {
        String bucketName = properties.getBucketName();
        if (client.doesBucketExistV2(bucketName)) {
            System.out.println("s3桶 bucket:" + bucketName + " 已存在,不需要新建桶=============");
        }

        String key = IdUtil.fastUUID()+".csv";
        System.out.println(key);
//        client.putObject(bucketName, key, FileUtil.file("C:\\Users\\fulls\\Desktop\\经典data_X.csv"));
//        S3Object object = client.getObject(bucketName, key);
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initResponse = client.initiateMultipartUpload(initRequest);
        String uploadId = initResponse.getUploadId();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(FileUtil.getInputStream("C:\\Users\\fulls\\Desktop\\经典data_X.csv"));
        UploadPartRequest uploadRequest = new UploadPartRequest()
                .withBucketName(bucketName)
                .withKey(key)
                .withUploadId(uploadId)
                .withPartNumber(1)
                .withInputStream(bufferedInputStream)
                .withPartSize(bufferedInputStream.available());
        UploadPartResult uploadPartResult = client.uploadPart(uploadRequest);
        PartETag partETag = uploadPartResult.getPartETag();

        List<PartETag> list = new ArrayList<>();
        list.add(partETag);
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, list);
        client.completeMultipartUpload(compRequest);
        S3Object object = client.getObject(bucketName, key);
        FileUtil.writeBytes(IoUtil.readBytes(object.getObjectContent()),"D:\\WorkSpace\\easyexcel\\minioClient\\oss数据.csv");
    }

    @GetMapping("/oss_download")
    public void test2() {
        String bucketName = properties.getBucketName();
        if (client.doesBucketExistV2(bucketName)) {
            System.out.println("s3桶 bucket:" + bucketName + " 已存在,不需要新建桶=============");
        }

        String key = "f06127536f0211ed909b02000a08038d.csv";

        S3Object object = client.getObject(bucketName, key);
        FileUtil.writeFromStream(object.getObjectContent(),"D:\\WorkSpace\\easyexcel\\minioClient\\oss数据.csv");


    }

    @PostMapping("/test")
    public void test4(@Valid @RequestBody TestParam param) {
        Console.log(param.getList());
    }

    @PostMapping("/test5")
    public String test5(@RequestBody User user) {
        System.out.println(DateUtil.now() + "============");
        return JSONUtil.toJsonStr(user);
    }

    @GetMapping("/")
    public Integer test5() {
        Console.log(DateUtil.now() + "接收到socket转发的请求================");
        return 2;
    }

    /**
     * @Valid错误示范，不起作用
     * @param userList
     */
    @PostMapping("/test1")
    public void test5(@Valid @NotEmpty(message = "不能为空") @RequestBody List<User> userList) {
        Console.log(userList);
    }




}
